package com.shj.em.biz.hr.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.biz.Constants;
import com.shj.em.common.to.Tree;
import com.shj.em.biz.hr.entity.Dept;
import com.shj.em.biz.hr.mapper.DeptMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;

/**
 * 部门信息 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-07-04
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    public Page<Dept> queryDeptPage(Map<String, Object> params){
        int offset = (Integer)params.get("offset");
        int pageSize = (Integer)params.get("limit");
        int pageNo = offset / pageSize + 1;
        Page<Dept> page = new Page(pageNo, pageSize);
        List<Dept> list = deptMapper.queryDeptPage(page, params);
        return page.setRecords(list);
    }

    public List<Dept> getAllDepts(){
        EntityWrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).orderBy("order_num", true);
        return deptMapper.selectList(wrapper);
    }

    public List<Dept> getChildDepts(Long id){
        return deptMapper.getChildDepts(id);
    }

    public boolean isDeptExist(String deptName){
        int cnt = deptMapper.getDeptByName(deptName, null);
        return cnt > 0;
    }

    public boolean isDeptExistExclude(String deptName, Long self){
        int cnt = deptMapper.getDeptByName(deptName, self);
        return cnt > 0;
    }

    public Tree<Dept> getDeptTree(){
        List<Tree<Dept>> nodes = new ArrayList<>();
        List<Dept> depts = getAllDepts();
        for (Dept dept : depts){
            Tree<Dept> node = new Tree<>();
            node.setId(dept.getId().toString());
            node.setParentId(dept.getParentId().toString());
            node.setName(dept.getDeptName());
            nodes.add(node);
        }
        List<Tree<Dept>> trees = Tree.buildTree(nodes, "0");

        // 添加根结点
        if (trees == null || trees.isEmpty()){
            return new Tree<>();
        }else{
            if(trees.size() == 1){
                return trees.get(0);
            }else{
                Tree<Dept> root = new Tree<Dept>();
                root.setId("-1");
                root.setParentId("");
                root.setHasParent(false);
                root.setChild(trees);
                return root;
            }
        }
    }

    public Dept findDeptByEmpId(Long empId){
        return deptMapper.findDeptByEmpId(empId);
    }

    public List<Dept> findAllProdDept(){
        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("parent_id", Constants.PROD_DEPT_ID);
        wrapper.orderBy("order_num", true);
        List<Dept> list = deptMapper.selectList(wrapper);
        return list;
    }

    public List<Dept> findAllProdGroupTwoDepts(){
        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("effort_type", "erbu");
        wrapper.orderBy("order_num", true);
        return deptMapper.selectList(wrapper);
    }
}
